package com.zrwl.poscloud.xo.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.EmployeeAccount;
import com.zrwl.poscloud.base.enums.EMessageModelType;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.global.Constants;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.mapper.EmployeeAccountMapper;
import com.zrwl.poscloud.xo.service.DeptAdminRService;
import com.zrwl.poscloud.xo.service.EmployeeAccountService;
import com.zrwl.poscloud.xo.service.MessageWxService;
import com.zrwl.poscloud.xo.vo.EmployeeAccountVO;
import com.zrwl.poscloud.xo.vo.MessageWxVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 持卡人员账户 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2021-07-26
 */
@Service
public class EmployeeAccountServiceImpl extends SuperServiceImpl<EmployeeAccountMapper, EmployeeAccount> implements EmployeeAccountService {

    @Autowired
    private EmployeeAccountService employeeAccountService;
    @Resource
    private EmployeeAccountMapper employeeAccountMapper;
    @Autowired
    private MessageWxService messageWxService;
    @Autowired
    private DeptAdminRService deptAdminRService;

    @Override
    public IPage<EmployeeAccountVO> getPageList(EmployeeAccountVO employeeAccountVO) {
        if (employeeAccountVO.getEmployeeUid() != null || employeeAccountVO.getCompanyCode() != null) {
            Page<EmployeeAccountVO> page = new Page<>();
            page.setCurrent(employeeAccountVO.getCurrentPage());
            page.setSize(employeeAccountVO.getPageSize());
            if(ArrayUtil.isNotEmpty(employeeAccountVO.getDeptRoleTree())){
                IPage<EmployeeAccountVO> pageList = employeeAccountMapper.getEmployeeAccountList(page, employeeAccountVO);
                List<EmployeeAccountVO> list = pageList.getRecords();
                pageList.setRecords(list);
                return pageList;
            }else {
                return null;
            }
        } else return null;
    }

    @Override
    public List<EmployeeAccount> getAllList(EmployeeAccountVO employeeAccountVO) {
        QueryWrapper<EmployeeAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<EmployeeAccount> list = employeeAccountService.list(queryWrapper);
        return list;
    }

    @Override
    public String add(EmployeeAccountVO employeeAccountVO) {
        EmployeeAccount entity = new EmployeeAccount();
        BeanUtils.copyProperties(employeeAccountVO, entity, SysConf.STATUS);
        entity.insert();
        employeeAccountVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public boolean edit(EmployeeAccountVO employeeAccountVO) {
        EmployeeAccount entity = employeeAccountService.getById(employeeAccountVO.getUid());
        BeanUtils.copyProperties(employeeAccountVO, entity);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return true;
    }

    @Override
    public String delete(EmployeeAccountVO employeeAccountVO) {
        EmployeeAccount entity = employeeAccountService.getById(employeeAccountVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        //entity.setUpdateTime//已通过填充实现，后续可删除(new Date());
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    //使用此service自动增加对应账户
    public EmployeeAccount customizeSelect(EmployeeAccountVO employeeAccountVO) {
        QueryWrapper<EmployeeAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_uid", employeeAccountVO.getEmployeeUid());
        queryWrapper.eq("status", 1);
        queryWrapper.eq("type", employeeAccountVO.getType());
        EmployeeAccount employeeAccount = employeeAccountService.getOne(queryWrapper);
        if (employeeAccount == null) {
            EmployeeAccount entity = new EmployeeAccount();
            BeanUtils.copyProperties(employeeAccountVO, entity, SysConf.STATUS);
            entity.insert();
            return entity;
        } else {
            return employeeAccount;
        }
    }

    @Override
    public EmployeeAccountVO employeeAccountBuild(EmployeeAccountVO employeeAccountVO) {
        QueryWrapper<EmployeeAccount> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(employeeAccountVO.getEmployeeUid()))
            throw new RuntimeException("账户所属人UID不能为空");
        queryWrapper.eq("employee_uid", employeeAccountVO.getEmployeeUid());
        queryWrapper.eq("status", 1);
        queryWrapper.eq("type", employeeAccountVO.getType());
        queryWrapper.last("for update");//行锁定
        EmployeeAccount employeeAccount = employeeAccountService.getOne(queryWrapper);
        EmployeeAccountVO result = new EmployeeAccountVO();
        if (employeeAccount == null) {
            EmployeeAccount entity = new EmployeeAccount();
            BeanUtils.copyProperties(employeeAccountVO, entity, SysConf.STATUS);
            entity.setBalance(employeeAccountVO.getBalance());
            entity.insert();
            BeanUtils.copyProperties(entity, result);
            result.setExit(false);
        } else {
            BeanUtils.copyProperties(employeeAccount, result);
            result.setExit(true);
        }
        return result;
    }

    @Override
    public EmployeeAccountVO employeeAccountFind(EmployeeAccountVO employeeAccountVO) {
        QueryWrapper<EmployeeAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_uid", employeeAccountVO.getEmployeeUid());
        queryWrapper.eq("status", 1);
        queryWrapper.eq("type", employeeAccountVO.getType());
        queryWrapper.last("for update");//行锁定
        EmployeeAccount employeeAccount = employeeAccountService.getOne(queryWrapper);
        if (employeeAccount == null) {
            return null;
        } else {
            EmployeeAccountVO result = new EmployeeAccountVO();
            BeanUtils.copyProperties(employeeAccount, result);
            result.setExit(true);
            return result;
        }
    }

    @Override
    public EmployeeAccountVO employeeAccountIsItEffective(Map<String, String> row, String employeeUid) {

        if (StringUtils.isNotEmpty(row.get("电子钱包余额")) && Integer.parseInt(row.get("电子钱包余额")) != 0) {
            EmployeeAccountVO employeeAccountVO = new EmployeeAccountVO();
            employeeAccountVO.setEmployeeUid(employeeUid);
            employeeAccountVO.setType(1);
            employeeAccountVO.setBalance(0.0);
            EmployeeAccount employeeAccount = employeeAccountService.customizeSelect(employeeAccountVO);
            BeanUtils.copyProperties(employeeAccount, employeeAccountVO);
            return employeeAccountVO;
        }
        if (StringUtils.isNotEmpty(row.get("补贴账户余额")) && Integer.parseInt(row.get("补贴账户余额")) != 0) {
            EmployeeAccountVO employeeAccountVO = new EmployeeAccountVO();
            employeeAccountVO.setEmployeeUid(employeeUid);
            employeeAccountVO.setType(2);
            employeeAccountVO.setBalance(0.0);
            EmployeeAccount employeeAccount = employeeAccountService.customizeSelect(employeeAccountVO);
            BeanUtils.copyProperties(employeeAccount, employeeAccountVO);
            return employeeAccountVO;
        }
        return null;
    }

    @Override
    public void checkAccountBalance(double balance) {
        List<EmployeeAccountVO> employeeAccountVOS = employeeAccountMapper.checkAccountBalance(balance);
        for (EmployeeAccountVO employeeAccountVO : employeeAccountVOS) {
            MessageWxVO messageWxVO = new MessageWxVO();
            messageWxVO.setFirst("您关注的持卡人" + employeeAccountVO.getEmployeeName() + "的账户余额不足，希望您尽快充值!");
            messageWxVO.setRemark("感谢你的使用。");
            String content = "";
            switch (employeeAccountVO.getType()) {
                case Constants.EMPLOYEE_ACCOUNT_AMT://余额账户
                    content += "余额账户,";
                    break;
            }
            content += employeeAccountVO.getBalance();
            messageWxVO.setContent(content);
            messageWxVO.setUrl("http://wx.yucard.cn");
            messageWxVO.setOpenId("obYcz6g4B52onGF1Qh8n2K9PRzYM");
            messageWxVO.setToUid(employeeAccountVO.getUserUid());
            messageWxVO.setModelType(EMessageModelType.WX.getType());
            messageWxService.add(messageWxVO, EMessageModelType.WX_INSUFFICIENT_BALANCE_REMINDER);
        }
    }

    @Override
    public List<EmployeeAccountVO> getEmployeeAccountInfo(String employeeUid) {
        return employeeAccountMapper.getEmployeeAccountInfo(employeeUid);
    }

    @Override
    public Map<String, Double> getAllAccountBalances(String employeeUid) {
        Map<String, Double> map = new HashMap<>();
        List<EmployeeAccountVO> employeeAccountInfo = employeeAccountMapper.getEmployeeAccountInfo(employeeUid);
        for (EmployeeAccountVO employeeAccountVO : employeeAccountInfo) {
            switch (employeeAccountVO.getType()){
                case Constants.EMPLOYEE_ACCOUNT_AMT://余额账户类型
                    map.put("balance_amt",employeeAccountVO.getBalance());
                    break;
                case Constants.EMPLOYEE_ACCOUNT_SUBSIDY_AMT://补贴账户类型
                    map.put("subsidy_amt",employeeAccountVO.getBalance());
                    break;
                case Constants.EMPLOYEE_ACCOUNT_COUNT://次数账户类型
                    map.put("count_amt",employeeAccountVO.getBalance());
                    break;
                case Constants.EMPLOYEE_ACCOUNT_SECOND_AMT://副账户类型
                    map.put("second_amt",employeeAccountVO.getBalance());
                    break;
                case Constants.EMPLOYEE_ACCOUNT_GIFT_AMT://赠送账户类型
                    map.put("gift_amt",employeeAccountVO.getBalance());
                    break;
                case Constants.EMPLOYEE_ACCOUNT_FEE_AMT://押金账户类型
                    map.put("fee_amt",employeeAccountVO.getBalance());
                    break;
            }
        }
        return map;
    }

    @Override
    public Date getSubsidyTime(String employeeUid) {
        EmployeeAccountVO employeeAccountVO = new EmployeeAccountVO();
        employeeAccountVO.setEmployeeUid(employeeUid);
        employeeAccountVO.setType(Constants.EMPLOYEE_ACCOUNT_SUBSIDY_AMT);
        return employeeAccountFind(employeeAccountVO) == null?new Date():employeeAccountFind(employeeAccountVO).getValidDate();
    }


}
